package top.eggcode.plugins.query.clause;

import top.eggcode.plugins.query.def.LiteralSentence;

/**
 * Title:
 * Description: TODO
 * Date: 2021/9/2 13:28
 *
 * @author JiaQi Ding
 * @version 1.0
 */
public class FromClause implements LiteralSentence {

    private String tableName;

    private String alias;

    public FromClause(String tableName) {
        this.tableName = tableName;
    }

    public FromClause as(String name) {
        this.alias = name;
        return this;
    }

    public String getReferenceName() {
        if (this.alias != null) {
            return this.alias;
        }
        return this.tableName;
    }

    public boolean existsAlias() {
        return this.alias != null;
    }

    @Override
    public String render() {

        StringBuilder fromClause = new StringBuilder("FROM ");
        fromClause.append(tableName);
        // 处理别名
        if (this.alias != null) {
            fromClause.append(" AS ").append(this.alias);
        }
        return fromClause.toString();
    }
}
